package Model;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;





import POJO.*;
public class OrderModel {
	public static ArrayList<OrderPOJO> DanhSachDonHang()
	{
	     Connection con = DataProvider.Connection();
	     OrderPOJO order=null;
	     ArrayList<OrderPOJO> listOrder= new  ArrayList<OrderPOJO>();
	     if(con != null)
	     {
	    	  try {
	                PreparedStatement ps;
	                ps  = con.prepareCall("select * from [order]");
	                ResultSet rs = ps.executeQuery();
	                while( rs.next()) {
	                	order = new OrderPOJO();
	                	order.setID(rs.getInt("id"));
	                	order.SetDate(rs.getString("date"));
	                	order.SetCustomerID(rs.getInt("customer_id"));
	                	order.SetTotal(rs.getFloat("total"));
	                	listOrder.add(order);
	                }
	                con.close();
	            } catch (SQLException ex) {
	                return null;
	            }    
	     }
	     return listOrder;
	}
	public static ArrayList<OrderPOJO> TimKiem(String Ser) throws UnsupportedEncodingException
	{
		Connection con = DataProvider.Connection();
		 OrderPOJO order=null;
	     ArrayList<OrderPOJO> listOrder= new  ArrayList<OrderPOJO>();
		 {
	    	  try {
	                PreparedStatement ps;
	                if(Ser != "")
	                {
	               
	                ps  = con.prepareCall("select * from [order] where id Like ? ");
	                byte pname[]=Ser.getBytes("ISO-8859-1");
	                String value1= new String(pname,"UTF-8");
	                value1 = '%' + value1 + '%';
	                ps.setString(1,value1);
	                }
	                else
	                {
	                	 ps  = con.prepareCall("select * from [order] ");
	                }
	                ResultSet rs = ps.executeQuery();
	                while( rs.next()) {
	                	order = new OrderPOJO();
	                	order.setID(rs.getInt("id"));
	                	order.SetDate(rs.getString("date"));
	                	order.SetCustomerID(rs.getInt("customer_id"));
	                	order.SetTotal(rs.getFloat("total"));
	                	listOrder.add(order);
	                }
	                con.close();
	            } catch (SQLException ex) {
	                return null;
	            }    
	     }
	     return listOrder;
	}
}	
